<?php
class system_status implements ArrayAccess, SeekableIterator, Countable {
	protected $_data = array();
	protected $_rows = array();
	protected $_index = array();
	protected $_position = 0;
	protected $_count = 0;
	protected $_dataIndex = array();
	
	protected $_default;
	
	public function getDataIndex($id) {
		foreach($this->_dataIndex as $ke => $v) {
			if($v == $id) {
				return $ke;
			}
		}
	}
	// ArrayAccess
	public function offsetExists($offset) {
		return isset($this->_dataIndex[$offset]);
	}
	
	public function offsetGet($offset) {
		if(!isset($this->_data[$offset])) {
			throw new system_exception("Illegal index $offset");
		}
		return $this->current();
	}
	public function offsetSet($id, $value) {
		$this->_count++;
		$this->_data[$id] = $value;
		$this->_index[strtoupper($value)] = $id;
		$this->_dataIndex[] = $id; // 登 记追加索引记录
	}
	public function offsetUnset($id) {
		$this->_count--;
		unset($this->_dataIndex[$this->getDataIndex($id)]);
		unset($this->_data[$id]);
	}
	public function getIterator() {
		return new ArrayIterator($this->_data);
	}
	
	// SeekableIterator
	public function seek($position) {
		$position = intval($position);
		if($position < 0 || $position >= $this->_count) {
			throw new system_exception("llegal index $position");
		}
		$this->_position = $position;
	}
	
	public function current() {
		if(!$this->valid()) {
			return null;
		}
		
		if(!isset($this->_rows[$this->_dataIndex[$this->_position]]) || empty($this->_rows[$this->_dataIndex[$this->_position]])) {
			$class = get_class($this);
			$this->_rows[$this->_dataIndex[$this->_position]] = new $class($this->_dataIndex[$this->_position]);
		}
		return $this->_rows[$this->_dataIndex[$this->_position]];
	}
	
	public function key() {
		return $this->_dataIndex[$this->_position];
	}
	
	public function next() {
		++$this->_position;
	}
	
	public function rewind() {
		$this->_position = 0;
		return $this;
	}
	public function valid() {
		return $this->_position >= 0 && $this->_position < $this->_count;
	}
	public function count() {
		return $this->_count;
	}
	
	public function __construct($status = null) {
		$this->init();
		if(null !== $status) {
			if($status instanceof system_status) {
				$status = $status->getId();
			} elseif(is_numeric($status)) {
				//
				if(!isset($this->_data[$status])) {
					throw new system_exception('The status ID :' . $status . ' can not defined');
				}
			} elseif(is_string($status)) {
				if(isset($this->_index[strtoupper($status)])) {
					$status = $this->getId($status);
				} else {
					throw new system_exception('The status key : ' . $status . ' can not be defined');
				}
			}
			$this->_default = $status;
		}
		$this->_count = count($this->_data);
	}
	
	public function getId($key = null) {
		if(null == $key) {
			$key = $this->_default;
		}
		if(is_numeric($key)) {
			return $key;
		} else {
			$key = strtoupper($key);
			if(isset($this->_index[$key])) {
				return $this->_index[$key];
			} else {
				throw new system_exception('The key:' . $key . ' is not defined');
			}
		}
	}
	
	public function getKey($id = null) {
		if(null == $id) {
			$id = $this->_default;
		}
		if(is_numeric($id)) {
			foreach($this->_index as $k => $v) {
				if($v == $id) {
					return $k;
				}
			}
		} else {
			$id = strtoupper($id);
			if(isset($this->_index[$id])) {
				return $id;
			}
		}
		throw new system_exception('The id :' . $id . ' is not defined');
	}
	
	public function getName($key = null) {
		$id = $this->getId($key);
		
		return $this->_data[$id];
	}
	
	/**
	 * 设置状诚
	 * 
	 * @param int $id
	 *        	状态ID编号
	 * @param string $key
	 *        	状态唯一的索引值（字母,不分大小写)
	 * @param string $title
	 *        	状态显示标题 可不设，为空时，显示索引值
	 */
	public function set($id, $key, $name = null) {
		if($name == null) {
			$name = $key;
		}
		$this->_data[$id] = $name;
		$this->_index[strtoupper($key)] = $id;
		$this->_dataIndex[] = $id;
		return $this;
	}
	
	public function setDefault($id) {
		$this->_default = $id;
		return $this;
	}
	
	public function is($status) {
		if(is_numeric($status) || is_int($status)) {
			return $this->getId() == $status;
		} elseif(is_string($status)) {
			return $this->getKey() == strtoupper($status);
		} elseif($status instanceof system_status) {
			return $status->getId() == $this->getId();
		} else {
			return false;
		}
	}
	/**
	 * 是否存在状态
	 * 
	 * @param unknown_type $status        	
	 */
	public function has($status) {
		if(is_numeric($status) || is_int($status)) {
			foreach($this->_index as $k => $v) {
				if(intval($v) === intval($status)) {
					return true;
				}
			}
		} elseif(is_string($status)) {
			$status = strtoupper($status);
			return isset($this->_index[$status]);
		} elseif(is_object($status)) {
			return $this->has($status->getKey());
		}
		return false;
	}
	public function exists($status) {
		foreach($this as $v) {
			if($v->is($status)) {
				return true;
			}
		}
		return false;
	}
	
	// 传入数组，检则当前状态是否在数组中
	public function in($status) {
		$class = get_class($this);
		;
		foreach($status as $k) {
			$tmp = new $class($k);
			if($tmp->is($this->getKey())) {
				return true;
			}
		}
		
		return false;
	}
	public function notIn(array $status) {
	
	}
	
	public function init() {
	}
	
	public function toArray() {
		$list = array();
		foreach($this as $k => $v) {
			$list[$k] = $v;
		}
		return $list;
	}
	
	public function __toString() {
		return $this->getName();
	}
}